package com.hbnu.test.www.net;

import java.io.*;
import java.net.Socket;

public class TCP_client06 {
    public static void main(String[] args) {
        if(args.length !=-1){
            System.out.println("请上传一张图片的全路径地址");
            return;
        }
        File file = new File(args[0]);
        if(!(file.isFile())||file.isDirectory()){
            System.out.println("请上传图片文件而不是文件夹");
        return;
        }
        if(!file.getName().endsWith(".jpg")){
            System.out.println("请上传.jpg格式的图片");
        }
        try (Socket socket = new Socket("127.0.0.1", 8888);FileInputStream fileInputStream = new FileInputStream(file);) {

            OutputStream out = socket.getOutputStream();
            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

            int len = 0;
            byte[] bytes = new byte[1024];
            while((len = fileInputStream.read(bytes))!=-1){
                out.write(bytes,0,len);
            }
            socket.shutdownOutput();

            String result = in.readLine();
            System.out.println("图片上传结果：" + result);
        }  catch (IOException e) {
            e.printStackTrace();
        }

    }
}
